# Maintainer: Haroogan <Haroogan@gmail.com>

_realname=emacs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=24.5
pkgrel=6
pkgdesc="The extensible, customizable, self-documenting, real-time display editor (mingw-w64)"
url="https://www.gnu.org/software/${_realname}/"
license=('GPL3')
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-ctags"
         "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-imagemagick"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
optdepends=("${MINGW_PACKAGE_PREFIX}-giflib"
            "${MINGW_PACKAGE_PREFIX}-gnutls"
            "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
            "${MINGW_PACKAGE_PREFIX}-libpng"
            "${MINGW_PACKAGE_PREFIX}-librsvg"
            "${MINGW_PACKAGE_PREFIX}-libtiff"
            "${MINGW_PACKAGE_PREFIX}-libxml2"
            "${MINGW_PACKAGE_PREFIX}-xpm-nox"
            "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-xpm-nox")
options=('strip')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
        'image.c.diff'
        'lread.c.diff'
        'Makefile.in.diff'
        'emacs-24.5-w32-ime.diff'
        '0001-gnutls.patch')
sha256sums=('dd47d71dd2a526cf6b47cb49af793ec2e26af69a0951cc40e43ae290eacfc34e'
            'SKIP'
            '4571d45ec26fd556e73a70bb0ab0a2a8fa1efc5e3b3c5b472ab68bb7dc9bf52c'
            '24b2201aa2e965c759cfc9539aa4b610d97f1d0240d9888e335da490b87b84b5'
            '1a49a2d4711d0204db80a88c120510168b2eb7649888de65634da252f03b7f16'
            '08e3863250aa88bfd54915fe1b5cb14b6481d4cf300669998d54e016563af3f2'
            '3a28e153e3934e5a96aaa762f181c606f0bb46684730fe9a0a34c67869cb6762')

prepare() {
  cd "${_realname}-${pkgver}"
  patch --binary --forward -p0 < "${srcdir}/image.c.diff"
  patch --binary --forward -p0 < "${srcdir}/lread.c.diff"
  patch --binary --forward -p0 < "${srcdir}/Makefile.in.diff"
  [[ -f "lisp/international/w32-ime.el" ]] && rm -rf "lisp/international/w32-ime.el"
  patch --binary --forward -p0 < "${srcdir}/emacs-24.5-w32-ime.diff"
  patch -p1 -i ${srcdir}/0001-gnutls.patch
  ./autogen.sh
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "build-${MINGW_CHOST}"

  local with_wide_int='no'

  if test "${CARCH}" == 'x86_64'; then
    with_wide_int='yes'
  fi

  CPPFLAGS="-DNDEBUG -isystem ${MINGW_PREFIX}/include"
  CFLAGS="-pipe -O3 -fomit-frame-pointer -funroll-loops"
  LDFLAGS="-s -Wl,-s"
  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --with-wide-int="${with_wide_int}" \
    --with-sound="yes" \
    --with-file-notification="yes" \
    --without-gpm \
    --without-gconf \
    --without-gsettings \
    --without-selinux

  make
}

package() {
  cd "build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  rm -f "${pkgdir}${MINGW_PREFIX}/bin/ctags.exe"
  rm -f "${pkgdir}${MINGW_PREFIX}/share/man/man1/ctags.1.gz"

  local dir="${pkgdir}${MINGW_PREFIX}/share/${_realname}"
        dir="${dir}/$(ls -1 ${dir} | grep -E '([0-9]+\.[0-9]+)(\.[0-9]+)?')/src"

  mkdir -p "${dir}"
  cd "${srcdir}/${_realname}-${pkgver}/src"
  cp *.c *.h *.m "${dir}"
}

# TODO:
# Patch `shell-file-name' default in the C source code similarly to
# `source-directory'.
